/*8f92WYTvNvqLOa023aQCKaJCO3P+RjQsCPBh4jkvdvUYVh63nmwLhndXx0Vy2oEE*/
/*
 * @Author: xiahui
 * @PageInfo: 采购合同维护卡片态按钮控制
 * @Date: 2019-01-10 18:17:35
 * @Last Modified by: tianzhyw
 * @Last Modified time: 2022-05-12 13:40:40
 */
import {
	BUTTONID,
	AREA,
	URL,
	UISTATUS,
	FSTATUSFLAG,
	FIELDS,
	VERSION,
	DELETELINEBTNS,
	CARDTABLEAREAIDS
} from '../../constance';
import { showSagaErrorToast } from '../../../../../scmpub/scmpub/pub/tool/sagaMessageUtils';
import { clearCardData } from '../../utils/cardPageUtil';
function buttonControl(props, pageStatus) {
	let status = pageStatus ? pageStatus : props.getUrlParam('status'); // 页面状态
	let source = props.getUrlParam('source'); // 来源页
	// 1、设置页面状态
	setUIState(props, status);
	// 2、按钮状态控制
	setButtonState(props, status, source, this.approveFlag);
	// 3、主按钮设置
	// setMainButton(props, status);
	// 4、返回按钮控件状态控制
	setHeadInfoState(props, status, source, this.approveFlag);
	// 5、卡片分页器状态控制
	setCardPaginationState(props, status, this.approveFlag);
	//
	setSagaBtnState(props, status);
}

function setUIState(props, status) {
	let uistatus =
		status == UISTATUS.add || status == UISTATUS.copy || status == UISTATUS.ref20 || status == UISTATUS.ref28
			? UISTATUS.add
			: status == UISTATUS.browse ? UISTATUS.browse : UISTATUS.edit;
	props.form.setFormStatus(AREA.cardFormId, uistatus);

	uistatus = status == UISTATUS.browse ? status : UISTATUS.edit;
	CARDTABLEAREAIDS.forEach((areaId) => {
		props.cardTable.setStatus(areaId, uistatus);
	});

	let isDisabled = status != UISTATUS.add;
	props.form.setFormItemsDisabled(AREA.cardFormId, { [FIELDS.pk_org_v]: isDisabled }); // 设置主组织编辑性
}

function setButtonState(props, status, source, approveFlag) {
	let fstatusflag = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.fstatusflag).value; // 合同状态
	let version = parseInt(props.form.getFormItemsValue(AREA.cardFormId, FIELDS.version).value); // 版本号
	if (status == UISTATUS.modify) {
		// 变更，币种不可编辑
		props.form.setFormItemsDisabled(AREA.cardFormId, { [FIELDS.corigcurrencyid]: true });
	}
	//编辑态设置暂存按钮不可用
	// if (status == UISTATUS.edit || approveFlag) {
	// 	props.button.setDisabled({ [BUTTONID.TemporaryStorage]: true });
	// } else {
	// 	props.button.setDisabled({ [BUTTONID.TemporaryStorage]: false });
	// }

	let visible = source == URL.ref20 || source == URL.ref28;

	if (visible || approveFlag) {
		props.button.setButtonVisible([ BUTTONID.ShowDraft ], false); // 转单不显示草稿按钮
		props.button.setButtonVisible([ BUTTONID.TemporaryStorage ], true);
	} else {
		props.button.setButtonVisible([ BUTTONID.TemporaryStorage ], true);
		props.button.setButtonVisible([ BUTTONID.ShowDraft ], true); //设置草稿按钮可用
	}

	props.button.setButtonVisible([ BUTTONID.Quit ], visible); // 退出转单
	let channelType = props.getUrlParam('channelType'); //推单标识
	if (channelType && channelType == 'priceaudit') {
		//价格审批单推单，不显示 退出转单按钮
		props.button.setButtonVisible([ BUTTONID.Quit ], false); // 退出转单
	}
	visible =
		status == UISTATUS.browse &&
		version == VERSION.v1 &&
		(fstatusflag == FSTATUSFLAG.free ||
			fstatusflag == FSTATUSFLAG.unapprove ||
			(fstatusflag === FSTATUSFLAG.approving && (approveFlag || props.getUrlParam('scene') == 'zycl')));
	props.button.setButtonVisible([ BUTTONID.Edit ], visible); // 修改

	visible = status == UISTATUS.browse && fstatusflag == FSTATUSFLAG.free && version == VERSION.v1;
	props.button.setButtonVisible([ BUTTONID.Delete ], visible); // 删除

	visible = status == UISTATUS.browse && fstatusflag == FSTATUSFLAG.free;
	props.button.setButtonVisible([ BUTTONID.Commit ], visible); // 提交

	visible = status == UISTATUS.browse && fstatusflag == FSTATUSFLAG.validate;
	props.button.setButtonVisible([ BUTTONID.SendMail ], visible); // 发送邮件

	visible = status == UISTATUS.browse && (fstatusflag == FSTATUSFLAG.approving || fstatusflag == FSTATUSFLAG.approve);
	props.button.setButtonVisible([ BUTTONID.UnCommit ], visible); // 收回

	visible = status == UISTATUS.browse && fstatusflag == FSTATUSFLAG.approve;
	props.button.setButtonVisible([ BUTTONID.Validate ], visible); // 生效

	visible = status == UISTATUS.browse && fstatusflag == FSTATUSFLAG.validate;
	props.button.setButtonVisible([ BUTTONID.UnValidate, BUTTONID.Freeze, BUTTONID.End ], visible); // 取消生效|冻结|终止

	visible = status == UISTATUS.browse && fstatusflag == FSTATUSFLAG.frozen;
	props.button.setButtonVisible([ BUTTONID.UnFreeze ], visible); // 解冻

	visible = status == UISTATUS.browse && fstatusflag == FSTATUSFLAG.terminate;
	props.button.setButtonVisible([ BUTTONID.UnEnd ], visible); // 取消终止

	visible =
		status == UISTATUS.browse &&
		(fstatusflag == FSTATUSFLAG.validate ||
			(version != VERSION.v1 &&
				(fstatusflag == FSTATUSFLAG.free ||
					fstatusflag == FSTATUSFLAG.unapprove ||
					(fstatusflag === FSTATUSFLAG.approving && approveFlag))));
	props.button.setButtonVisible([ BUTTONID.Modify ], visible); // 变更
	if (approveFlag) {
		props.button.setButtonVisible([ BUTTONID.Modify ], false); // 变更
	}

	visible = status == UISTATUS.browse && fstatusflag == FSTATUSFLAG.free && version != VERSION.v1;
	props.button.setButtonVisible([ BUTTONID.ModifyDelete ], visible); // 变更删除

	visible =
		status == UISTATUS.browse &&
		(fstatusflag == FSTATUSFLAG.validate ||
			fstatusflag == FSTATUSFLAG.frozen ||
			fstatusflag == FSTATUSFLAG.terminate);
	props.button.setButtonVisible([ BUTTONID.PayPlan ], visible); // 付款计划

	props.button.setButtonVisible([ BUTTONID.LinkPriceInfo ], true); // 联查价格明细
	props.button.setButtonVisible(
		[
			BUTTONID.PasteLineToTail,
			BUTTONID.PasteLineToTailTerm,
			BUTTONID.PasteLineToTailPay,
			BUTTONID.PasteLineToTailFee,
			BUTTONID.PasteLineToTailMemora,
			BUTTONID.CancelB,
			BUTTONID.CancelBTerm,
			BUTTONID.CancelBPay,
			BUTTONID.CancelBFee,
			BUTTONID.CancelBMemora
		],
		false
	);
	props.button.setButtonVisible(
		[
			BUTTONID.ApproveInfo,
			BUTTONID.Assitfunc,
			// BUTTONID.ControlScope,
			BUTTONID.BatchControlScope,
			// BUTTONID.PayPlan,
			BUTTONID.StructFile,
			BUTTONID.File,
			BUTTONID.ModifyHistory,
			BUTTONID.BillLinkQuery,
			BUTTONID.QueryAboutBusiness,
			BUTTONID.PayExecState,
			BUTTONID.Print,
			BUTTONID.Output,
			BUTTONID.PrintCountQuery,
			BUTTONID.Refresh
		],
		status == UISTATUS.browse
	);

	// 合同控制范围在冻结和终止状态下不可见
	props.button.setButtonVisible(
		[ BUTTONID.ControlScope ],
		status == UISTATUS.browse && fstatusflag != FSTATUSFLAG.frozen && fstatusflag != FSTATUSFLAG.terminate
	);

	// 共享中心显示审批详情
	props.button.setButtonVisible([ BUTTONID.ApproveInfo ], status == UISTATUS.browse && !approveFlag);

	// 转单时，新增组以及复制按钮状态不可见
	props.button.setButtonVisible(
		[ BUTTONID.Add, BUTTONID.Manual, BUTTONID.Ref20, BUTTONID.Ref28, BUTTONID.Copy ],
		status == UISTATUS.browse && !source
	);

	props.button.setButtonVisible(
		[
			BUTTONID.Save,
			BUTTONID.SaveCommit,
			BUTTONID.Cancel,
			BUTTONID.LineGroup,
			BUTTONID.AddLine,
			BUTTONID.AddLineTerm,
			BUTTONID.AddLinePay,
			BUTTONID.AddLineFee,
			BUTTONID.AddLineMemora,
			BUTTONID.DeleteLine,
			BUTTONID.DeleteLineTerm,
			BUTTONID.DeleteLinePay,
			BUTTONID.DeleteLineFee,
			BUTTONID.DeleteLineMemora,
			BUTTONID.CopyLine,
			BUTTONID.CopyLineTerm,
			BUTTONID.CopyLinePay,
			BUTTONID.CopyLineFee,
			BUTTONID.CopyLineMemora,
			BUTTONID.ReRangeRowNo
		],
		status != UISTATUS.browse
	);

	// if (source == URL.ref20 || source == URL.ref28) {
	// 	props.button.setButtonVisible([ BUTTONID.TemporaryStorage ], false); // 转单不显示草稿按钮
	// }

	if (status == UISTATUS.browse) {
		props.button.setButtonVisible([ BUTTONID.TemporaryStorage ], false);
	}

	props.button.setButtonVisible([ BUTTONID.ImageScan, BUTTONID.ImageView ], status == UISTATUS.browse); // 影像查看，扫描
	onClickModify.call(this, props, status == UISTATUS.modify ? true : false);
}

function setHeadInfoState(props, status, source, approveFlag) {
	let scene = props.getUrlParam('scene');
	// 共享中心参数zycl,报账平台参数bz,单据稽核zycx
	let visible =
		(!approveFlag && status == UISTATUS.browse && scene != 'zycx' && scene != 'zycl' && scene != 'bz') ||
		source == URL.ref20 ||
		source == URL.ref28;
	let vbillcode = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.vbillcode).value; // 单据号
	props.BillHeadInfo.setBillHeadInfoVisible({
		showBackBtn: visible,
		showBillCode: true,
		billCode: vbillcode
	});
}

function setCardPaginationState(props, status, approveFlag) {
	let scene = props.getUrlParam('scene');
	// 共享中心参数zycl,报账平台参数bz
	let visible = !approveFlag && status == UISTATUS.browse && scene != 'zycl' && scene != 'bz';
	props.cardPagination.setCardPaginationVisible('cardPaginationBtn', visible);
}

/**
 * 设置卡片肩部按钮可用性：删行、复制行
 */
function onRowsSelected(props, moduleId) {
	let buttonArr = DELETELINEBTNS[moduleId];
	let checkedRows = props.cardTable.getCheckedRows(moduleId);
	if (checkedRows && checkedRows.length > 0) {
		props.button.setButtonDisabled(buttonArr, false);
	} else {
		props.button.setButtonDisabled(buttonArr, true);
	}
	if (moduleId == AREA.cardPayId && UISTATUS.modify == props.getUrlParam('status')) {
		// 变更时付款协议按钮不可用
		buttonArr.push(BUTTONID.AddLinePay);
		props.button.setButtonDisabled(buttonArr, true);
	}
}

/**
 * 点击变更置灰付款协议按钮
 * ADD BY CONGKE
 * @param {} props
 */
function onClickModify(props, isDisabled) {
	props.button.setButtonDisabled([ BUTTONID.AddLinePay ], isDisabled);
}

/**
 * 无数据时，设置卡片空界面
 * @param {*} props
 */
function setEmptyValueDisplay(props) {
	clearCardData(props); // 清空卡片数据
	setUIState(props, UISTATUS.browse); // 设置卡片编辑性

	props.BillHeadInfo.setBillHeadInfoVisible({
		showBackBtn: true,
		showBillCode: false
	});
	// 设置按钮可见性
	props.button.setButtonVisible([ BUTTONID.Add, BUTTONID.Manual, BUTTONID.Ref20, BUTTONID.Ref28 ], true);
	props.button.setButtonVisible(
		[
			BUTTONID.Edit,
			BUTTONID.Delete,
			BUTTONID.Copy,
			BUTTONID.ApproveInfo,
			BUTTONID.Commit,
			BUTTONID.UnCommit,
			BUTTONID.Execute,
			BUTTONID.Validate,
			BUTTONID.UnValidate,
			BUTTONID.Freeze,
			BUTTONID.UnFreeze,
			BUTTONID.End,
			BUTTONID.UnEnd,
			BUTTONID.Modify,
			BUTTONID.ModifyDelete,
			BUTTONID.Assitfunc,
			BUTTONID.ControlScope,
			BUTTONID.BatchControlScope,
			BUTTONID.PayPlan,
			BUTTONID.StructFile,
			BUTTONID.File,
			BUTTONID.ModifyHistory,
			BUTTONID.BillLinkQuery,
			BUTTONID.QueryAboutBusiness,
			BUTTONID.PayExecState,
			BUTTONID.Print,
			BUTTONID.Output,
			BUTTONID.PrintCountQuery,
			BUTTONID.Refresh,

			BUTTONID.Save,
			BUTTONID.TemporaryStorage,
			BUTTONID.SaveCommit,
			BUTTONID.Cancel,
			BUTTONID.AddLine,
			BUTTONID.DeleteLine,
			BUTTONID.CopyLine,

			BUTTONID.ImageScan,
			BUTTONID.ImageView
		],
		false
	);
	setCardPaginationState(props, false);
}
function setSagaBtnState(props, status) {
	let sagaStatus = props.form.getFormItemsValue([ AREA.cardFormId ], FIELDS.sagaStatus);
	//获得saga状态 // 第一次进入卡片页，frozen为空，取界面sags_statue的值判断冻结状态
	let frozen = sagaStatus && sagaStatus.value == '1' ? true : false;
	// 设置回退、重试按钮状态，用来是否显示
	if (status == UISTATUS.browse && frozen) {
		props.button.toggleErrorStatus(AREA.card_head, {
			isError: true
		});
	} else {
		props.button.toggleErrorStatus(AREA.card_head, {
			isError: false
		});
	}
}

/**
 * 显示saga错误信息提示框
 * @param {*} props
 * @param {*} params
 */
function showSagaErrorToasts(props, formId, billpkName, params = {}) {
	let saga_gtxid = props.form.getFormItemsValue(formId, 'saga_gtxid');
	let billPk = props.form.getFormItemsValue(formId, billpkName);
	params.gtxid = saga_gtxid && saga_gtxid.value;
	params.billpk = billPk && billPk.value;
	showSagaErrorToast(props, params);
}

export { buttonControl, onRowsSelected, setEmptyValueDisplay, showSagaErrorToasts, onClickModify };

/*8f92WYTvNvqLOa023aQCKaJCO3P+RjQsCPBh4jkvdvUYVh63nmwLhndXx0Vy2oEE*/